//
//  MyTitleScrollerView.m
//  New3.0
//
//  Created by k on 16/6/24.
//  Copyright © 2016年 QIJIA. All rights reserved.
//

#import "MyTitleScrollerView.h"

#define MainScrollViewNotivation @"MainScrollViewNotivation"

@interface MyTitleScrollerView ()<UIScrollViewDelegate>
{
    NSInteger _num;
    
    float _maxX;

}

@property (nonatomic, strong) UIScrollView *mainScrollerView;

@property (nonatomic, strong) UILabel *titleLbl;

@property (nonatomic, strong) NSTimer *myTimer;

@end

@implementation MyTitleScrollerView

#pragma mark 初始化ScrollerView
-(UIScrollView *)mainScrollerView{
    if (!_mainScrollerView) {
        _mainScrollerView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _mainScrollerView.delegate = self;
        _mainScrollerView.showsHorizontalScrollIndicator = NO;
        _mainScrollerView.showsVerticalScrollIndicator = NO;
        _mainScrollerView.userInteractionEnabled = self.isTouch;
        
        _mainScrollerView.backgroundColor = WRGBColor(242, 242, 242);
        [self addSubview:_mainScrollerView];
    }
    return _mainScrollerView;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self == [super initWithCoder:aDecoder]) {
        self.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
        self.repeatTime = 10;
        self.fontSize = 16;
        self.isTouch = YES;
    }
    return self;
}


#pragma mark 展示
-(void)show{
    
    //设置自动行数与字符换行
    [self.titleLbl setNumberOfLines:1];
    
    // 测试字串
    NSString *s = [NSString stringWithFormat:@"%@  %@  ",self.titleStr,self.titleStr];
    
    UIFont *font = [UIFont systemFontOfSize:self.fontSize];
    
    //设置一个行高上限
    CGSize size = CGSizeMake(CGFLOAT_MAX,self.bounds.size.height);
    //计算实际frame大小，并将label的frame变成实际大小
    
    CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
    self.titleLbl.frame = CGRectMake(0, 0, labelsize.width, self.bounds.size.height);
    self.titleLbl.text = s;
    self.titleLbl.font = font;
    //设置文字的其他属性
    self.titleLbl.textColor = self.color;
    
//    self.mainScrollerView.contentSize = CGSizeMake(labelsize.width, labelsize.height);
    
    self.mainScrollerView.contentSize = CGSizeMake(labelsize.width, 14);

    [self.mainScrollerView addSubview:self.titleLbl];
    
    CGPoint bottomOffset = CGPointMake(self.mainScrollerView.contentSize.width/2 - self.mainScrollerView.bounds.size.width, self.mainScrollerView.contentOffset.y);
    
    _maxX = self.mainScrollerView.contentSize.width/2;
    
    //设置延迟时间
//    float scrollDurationInSeconds = self.repeatTime;
    
    //计算timer间隔
    float scrollDurationInSeconds = self.repeatTime /100.0;
    
    if (!self.myTimer) {
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:scrollDurationInSeconds target:self selector:@selector(timeRepeat:) userInfo:nil repeats:YES];
    }
    
    [self.myTimer setFireDate:[NSDate distantPast]];

    //宽度小于最大宽度，则不滚动
    if (_maxX <self.bounds.size.width) {
        [self.myTimer setFireDate:[NSDate distantFuture]];
        self.titleLbl.text = self.titleStr;
    }
}

#pragma mark 计时器
-(void)timeRepeat:(NSTimer *)timer{
    
    CGPoint newScrollViewContentOffset = self.mainScrollerView.contentOffset;
    
    //每次timer向右移动距离 1px
    newScrollViewContentOffset.x += 1;

//    WLog(@"%f ------- %f",newScrollViewContentOffset.x,_maxX);

    newScrollViewContentOffset.x = MAX(newScrollViewContentOffset.x, 0);
    
    //如果到顶了，重复开始
    if (newScrollViewContentOffset.x >= _maxX) {
        newScrollViewContentOffset.x = 0;
    }
    
    self.mainScrollerView.contentOffset = newScrollViewContentOffset;
}

#pragma mark 关闭定时器
-(void)dismiss{
    [self.myTimer setFireDate:[NSDate distantFuture]];
}



@end
